//
//  TableUIViewDataController.swift
//  DemoUI
//  TableUIViewDataController  实战练习
//  Created by 白瑾浩 on 2022/1/9.
//

import UIKit

class TableUIViewDataController: UIViewController, UITableViewDelegate, UITableViewDataSource {
   
    
    var datas:[Product] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        //组装数据
        var product:Product
        for _ in 0...10{
            product = Product()
            product.imageName = "pImage"
            product.name = "杜康酒1500ml灌装"
            product.subTitle = "何以解忧，唯有杜康，纯粮酿造，国庆大酬宾"
            product.price = "59元"
            datas.append( product )
        }
    

        let tableView = UITableView(frame: self.view.frame, style: UITableView.Style.grouped )
        //注册cell
        tableView.register(UINib.init(nibName: "ProductTableTableViewCell", bundle: nil), forCellReuseIdentifier: "tableViewCellIdone")
        self.view.addSubview( tableView )
        
        //设计数据源
        tableView.delegate = self
        tableView.dataSource = self
        
        //开启编辑模式
        tableView.isEditing = true
        
        
        
        
    
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //匹配建立的 cell类
        let cell:ProductTableTableViewCell  = tableView.dequeueReusableCell(withIdentifier: "tableViewCellIdone", for: indexPath ) as! ProductTableTableViewCell
        let model = datas[indexPath.row]

        cell.iconView.image = UIImage(named: model.imageName! )
        cell.pIntro.text = model.subTitle!
        cell.price.text = model.price!
        cell.pTtitle.text = model.name!
        print( model.subTitle! )
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 183
    }
    
    //返回每行编辑cell的编辑模式
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        if( indexPath.row ==  0 ){
            return .insert
        }else{
            return .delete
        }
    }
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        if indexPath.row == 0 {
            return "插入"
        }else{
            return "删除"
        }
    }
    //根据点击的类型  进行不同功能的处理
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete{
            print( "点击了删除按钮" )
            
            datas.remove(at: indexPath.row) //删除某一条
            
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.left)
            
            
        }else{
            print( "点击了插入了按钮" )
            
            let product = Product()
            product.imageName = "pImage"
            product.name = "杜康酒1500ml灌装"
            product.subTitle = "何以解忧，唯有杜康，纯粮酿造，国庆大酬宾"
            product.price = "59元"
            //datas.append( product )
            datas.insert(product, at: indexPath.row)
            
            tableView.insertRows(at: [indexPath], with: UITableView.RowAnimation.fade)
            
        }
    }
    //实现改的功能,出现改的功能图标
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let tmp = datas[sourceIndexPath.row]
        let tmp2 = datas[destinationIndexPath.row]
        datas[destinationIndexPath.row] = tmp
        datas[sourceIndexPath.row] = tmp2
    }

}
